#include <stdio.h>   /* required for file operations */
#include <strings.h>

FILE *fr,*fw;            /* declare the file pointer */




int 
main(int argc, char *argv[])
{
	int n;
	long elapsed_seconds;
        char file_name[20];
        char record[100];

        int no_nodes = 0, no_values;
	char thput1[15],lat1[15],req1[15];
	double thput,lat,req;
	double sum_thput,sum_lat,sum_req;
	char line[80];
	if(argc !=2){
		printf("Error in number of arguments <file number>");

	}
	else
	{

		sprintf(file_name,"log%d",atoi(argv[1]));
		fr = fopen (file_name, "rt");  /* open the file for reading */
		fgets(line, 80, fr); 

		while((fgets(line, 80, fr) != NULL) && (no_values=sscanf(line ,"%lf %s %lf %s %lf %s",&thput,thput1,&lat,lat1,&req,req1)))
		{
			sum_thput += thput;
			sum_lat += lat;
			sum_req += req;
			no_nodes++;

		}
		fw = fopen("Results", "a");
		if (fw == NULL) {
			fprintf(stderr,"Can not open output file\n");
			return;
		}

		sprintf(record,"%d clients: Th= %lf %s : Lat= %lf %s : Req= %lf %s\n",atoi(argv[1]),sum_thput/no_nodes,thput1,sum_lat/no_nodes,lat1,sum_req/no_nodes,req1);
		int length = strlen(record);
		fwrite(record, length, 1, fw);
		fclose(fw); 
		fclose(fr);  /* close the file prior to exiting the routine */
	}
	return;
} /*of main*/
